2
2
.
.
6
6
.
.
1
1
H
H
2
2
I
I
n
n
f
f
o
o
[
[
G
G
]
]
This tutorial show how to use Repository that works with H2 Database.
Application Schema [Results]
Spring Boot Starters
GROUP
DEPENDENCY
DESCRIPTION
Web
Spring Web
Enables @Controller and @RequestMapping. Includes Tomcat Server.
SQL
Spring Data JPA
Enables @Entity and @Id
SQL
H2 Database
Enables in-memory H2 Database
P
P
r
r
o
o
c
c
e
e
d
d
u
u
r
r
e
e
Create Project: springboot_db_h2 (add Spring Boot Starters from the table)
Edit: application.properties (specify H2 DB name & enable H2 Web Console)
Create Package: entities (inside main package)
– Create Class: PersonEntity.java (inside package entities)
Create Package: repositories (inside main package)
– Create Interface: PersonRepository.java (inside package repositories)
Create Package: controllers (inside main package)
– Create Class: MyController.java (inside package controllers)
application.properties
# H2 DATABASE
spring.h2.console.enabled = true
spring.datasource.url = jdbc:h2:mem:testdb
MyController
http://localhost:8080/AddPerson
addPerson()
PersonEntity
H2
PersonEntity.java
package com.ivoronline.springboot_db_h2.entities;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class PersonEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Integer id;
public String name;
public Integer age;
}
PersonRepository.java
package com.ivoronline.springboot.repository_store_entity.respositories;
import com.ivoronline.springboot.repository_store_entity.entities.PersonEntity;
import org.springframework.data.repository.CrudRepository;
public interface PersonRepository extends CrudRepository<PersonEntity, Integer> { }
MyController.java
package com.ivoronline.springboot_db_h2.controllers;
import com.ivoronline.springboot_db_h2.entities.PersonEntity;
import com.ivoronline.springboot_db_h2.repositories.PersonRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class MyController {
@Autowired PersonRepository personRepository;
@ResponseBody
@RequestMapping("/AddPerson")
public String addPerson() {
//CREATE PERSON
PersonEntity personEntity = new PersonEntity();
personEntity.name = "John";
personEntity.age = 20;
//STORE PERSON
personRepository.save(personEntity);
//RETURN SOMETHING
return "Hello " + personEntity.name;
}
}
R
R
e
e
s
s
u
u
l
l
t
t
s
s
http://localhost:8080/AddPerson
http://localhost:8080/h2-console (Open H2 Console)
Application Structure
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>